package com.sensorfriendly.ui;

import java.util.List;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.sensorfriendly.AppConstants;
import com.sensorfriendly.AppContext;
import com.sensorfriendly.R;
import com.sensorfriendly.model.Scenario;
/**
 * Showing a list of sceanrios that user can customize
 *
 */
public class EditScenario extends ListActivity implements OnClickListener{

	private List<Scenario> scenarios;
	private ScenarioListAdapter adapter ;
	private ListView lv; 
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_scenario);
        
        ((TextView) findViewById(R.id.page_title)).setText("Edit Scenarios");
		((ImageView) findViewById(R.id.ab_settings)).setOnClickListener(this);
		((ImageView) findViewById(R.id.ab_home)).setOnClickListener(this);
		((ImageView) findViewById(R.id.ab_call)).setVisibility(View.GONE);
        
		final AppContext app = (AppContext) getApplication();
        
		scenarios = app.getAllScenarios();
        
		// Create the adapter
		adapter = new ScenarioListAdapter(this, scenarios);
		
		// Selecting single ListView item
		lv = getListView();

		lv.setAdapter(adapter);

		
		// On Click Listener for the ListView that opens the messages
		// under the category
		lv.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				
				Intent editMessagesActivity = new Intent(EditScenario.this,EditMessage.class);
				editMessagesActivity.putExtra(AppConstants.CATEGORY, scenarios.get(position).toString());
				startActivity(editMessagesActivity);
			}
		});
		
		
		/******************************************
		 * Handling for the Add button
		 ******************************************/
		// Get the button to add to the List of Scenarios
		Button btn = (Button)findViewById(R.id.btnAdd);
		// Add the new Category
		btn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				EditText newCategory = (EditText) findViewById(R.id.txtItem);
				
				// If there is no text, prompt the user
				if(newCategory.getText().toString().trim().matches(""))
				{
					Toast.makeText(EditScenario.this, "Please enter scenario name and then press 'Add'", Toast.LENGTH_SHORT).show();
					return;
				}
				
				app.addScenario(new Scenario(newCategory.getText().toString()));
				
				Intent editMessagesActivity = new Intent(EditScenario.this,EditMessage.class);
				editMessagesActivity.putExtra(AppConstants.CATEGORY, newCategory.getText().toString());
				newCategory.setText("");
				startActivity(editMessagesActivity);
			}
		});
    }
    
    /**
     * always update the listview when coming to this view.
     */
    @Override
    public void onResume(){
    	super.onResume();
		final AppContext app = (AppContext) getApplication();
		scenarios = app.getAllScenarios();
    	adapter.setScenarios(scenarios);
		adapter.notifyDataSetChanged();
    	
    	lv.setAdapter(adapter);
    }
    
    /**
     * handling buttons on the action bar.
     */
	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.ab_settings) {
			Intent settingsIntent = new Intent(this, SettingsActivity.class);
			startActivity(settingsIntent);
		} else if (v.getId() == R.id.ab_home) {
			Intent homeIntent = new Intent(this, DashboardNavigationActivity.class);
			homeIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
					| Intent.FLAG_ACTIVITY_SINGLE_TOP);
			startActivity(homeIntent);
		} 
	}
    
    @Override
    public void onPause(){
    	super.onPause();

    }
}
